<?php
namespace app\interfaces\controller;
use think\Db;
use think\Session;
use libs\ccmm;
class Bkuser extends Base{

    public function index(){
        return $this->fetch();
    }
	
	public function login()
	{
		return $this->fetch();
	}
	public function logins()
	{
		$username=input('username');
		$password=input('password');
		$rule = ['name'=> 'require',
		'mima'=> 'require',
		];
		$msg = [
		'name.require' => '用户名必须填写',
		'mima.require'   => '密码必须',
		];
		$data=[
		'name'=>$username,
		'mima'=>$password,
		];
		$m=$this->validate($data,$rule,$msg);
		$jsonStr='';
		if($m!=1)
		{
			echo $jsonStr .= '{"msg":"102","data":[],"content":"'.$m.'","success":"false"}';
			exit;
			//$this->success($m, 'User/login');
		}
		//$ms['password']='';
		$ms=Db::table('user')->where(array('username'=>$username))->find();
		if(md5(md5($password)) !=$ms['password'])
		{
			echo $jsonStr .= '{"msg":"102","data":[],"content":"密码或者用户名错误！","success":"false"}';
			exit;
			//$this->success('密码或者用户名错误','User/login');
		}
		//Session::set('userid',$ms['id']);
		//Session::set('username',$ms['username']);
		$str = '{
			"id":"' . $ms['id'] . '",
			"username":"' . $ms['username'] .'",
		"password":"'.$ms['password'].'",
		"headimg":"'.$ms['headimg'].'",
	"createTime":"'.$ms['createTime'].'"}';
	    $jsonStr = '{"msg":101,"data":[' . $str . '],"success":true}';
	    echo $jsonStr;
        exit;
		//echo "登陆成功";
		//exit;
	}

    //用户注册
    public function regist(){
        //接收前端表单提交的数据
        //$user_tel = input('post.tel');
        $user_name = input('username');
		$yanzhengma = input('yanzhengma');
        $user_passwd = input('password');
        $user_passwd1 = input('password1');
		$jsonStr='';
		
		$rule = ['name'=> 'require|max:11',
		'mima'=> 'require',
		'yanzheng'=> 'require',
		];
		$msg = [
		'name.require' => '名称必须填写',
		'name.max'     => '最多不能超过11个字符',
		'mima.require'   => '密码必须',
		'yanzheng.require' => '验证码必须',
		'yanzheng.captcha'=>'验证码不正确',
		];
		$data=[
		'name'=>$user_name,
		'mima'=>$user_passwd,
		'yanzheng'=>$yanzhengma,
		];
		$m=$this->validate($data,$rule,$msg);
		if($m!=1)
	{
		echo $jsonStr .= '{"msg":"102","data":[],"content":"'.$m.'","success":"false"}';
			exit;
		//$this->success($m, 'User/index');
	}
	if($user_passwd != $user_passwd1)
		{
			echo $jsonStr = '{"msg":"102","data":[],"content":"再次密码不一样！","success":"false"}';
			exit;
			//$this->success('再次密码不一样','User/index');
		}
	/*if($yanzhengma != Session::get('yznumber'))
	{
		echo $jsonStr .= '{"msg":"102","data":[],"content":"验证码错误","success":"false"}';
			exit;
		 //$this->success('验证码错误','User/index');
	}*/
	$ms=Db::table('user')->where(array('username'=>$user_name))->field('id')->select();
	//var_dump($ms);
	//exit;
	if($ms)
	{
		echo $jsonStr .= '{"msg":"102","data":[],"content":"账号已经注册","success":"false"}';
			exit;
		//$this->success('账号已经注册','User/index');
	}
        $data = array(
            'username' =>$user_name,
            'password' =>md5(md5($user_passwd)),
			'createTime'=>time(),
			'laterTime'=>time(),
        );
        //写入数据库
        $user = Db::table('User')->insert($data);
		$userid=Db::table('User')->getLastInsID();
		
		$m=new Huanxin;
        $m->registerhx($userid);
		$sql="select a.*,b.hxuname from user as a left join hxuser as b on a.id=b.uid where a.id=".$userid;
		$usdata=Db::query($sql);
		//$usdata=Db::table('User')->where(array('id'=>$userid))->find();
		$str = '{
			"id":"' . $usdata[0]['id'] . '",
			"username":"' . $usdata[0]['username'] .'",
		    "password":"'.$usdata[0]['password'].'",
		    "headimg":"'.$usdata[0]['headimg'].'",
			"hxuname":"'.$usdata[0]['hxuname'].'",
	        "createTime":"'.$usdata[0]['createTime'].'"}';
        if ($user) {
			echo $jsonStr .= '{"msg":"101","data":['.$str.'],"content":"注册成功","success":"true"}';
			exit;
            //return $this->success('注册成功','User/login');
        } else {
			echo $jsonStr .= '{"msg":"102","data":[],"content":"注册失败","success":"false"}';
			exit;
            //return $this->success('注册失败','User/index');
        }

    }
	//修改头像
	public function upheadimg()
	{
		//$this->islogin();
		//$imgurl=input('imgurl');
		$id=input('id');
		$file = request()->file('image');
    
    // 移动到框架应用根目录/public/uploads/ 目录下
    if($file){
        $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
        if($info){
            // 成功上传后 获取上传信息
            // 输出 jpg
            //echo $info->getExtension();
            // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
            $imgurl=$info->getSaveName();
            // 输出 42a79759f284b767dfcb2a0197904287.jpg
            //echo $info->getFilename(); 
        }else{
            // 上传失败获取错误信息
            echo $jsonStr .= '{"msg":"102","data":[],"content":"上传失败","success":"false"}';
			exit;
        }
		
    }
		if(empty($imgurl))
		{
			echo $jsonStr .= '{"msg":"102","data":[],"content":"头像不能为空","success":"false"}';
			exit;
			//$this->success('头像不能为空','User/index');
		}
		//$uid=Session::get('userid');
		$s=Db::table('user')->where(array('id'=>$id))->update(array('headimg'=>$imgurl));
		if($s)
		{
			echo $jsonStr .= '{"msg":"101","data":[],"content":"修改成功","success":"true"}';
			exit;
		}
		else
		{
			echo $jsonStr .= '{"msg":"102","data":[],"content":"修改失败","success":"false"}';
			exit;
		}
	}
	//设置昵称
	public function upnick()
	{
		//$this->islogin();
		$nickname=input('nickname');
		$id=input('id');
		if(empty($nickname))
		{
			echo $jsonStr .= '{"msg":"102","data":[],"content":"昵称不能为空","success":"false"}';
			exit;
		}
		//$uid=Session::get('userid');
		$s=Db::table('user')->where(array('id'=>$id))->update(array('nickname'=>$nickname));
		if($s)
		{
			echo $jsonStr .= '{"msg":"101","data":[],"content":"修改成功","success":"true"}';
			exit;
		}
		else
		{
			echo $jsonStr .= '{"msg":"102","data":[],"content":"修改失败","success":"false"}';
			exit;
		}
	}
    //手机验证码
    public function Verification(){
		$jsonStr='';
        $mobile = input('mobile');
		//var_dump($tel);
		//exit;
        //$num =  rand(1000,9999);
		$ms=Db::table('user')->where(array('username'=>$mobile))->find();
		//var_dump($ms);
		//exit;
		if($ms)
		{
			echo $jsonStr .= '{"msg":"102","data":[],"content":"账号已经注册","success":"false"}';
			exit;
		}
		$code = rand(10000,99999);
		$content="您的验证码是".$code."。如非本人操作，请忽略本短信";
		//echo $content;
		//exit;
		$msg_obj = new ccmm($mobile,$content);
		$msg_res = $msg_obj->fang();
		//$num =1234;
        Session::set('yznumber',$code);
		if($msg_res=='操作成功')
		{
		echo $jsonStr = '{"msg":"101","data":[],"content":"发送成功","success":"true"}';
			exit;
		}
		else
		{
			echo $jsonStr = '{"msg":"102","data":[],"content":"发送失败","success":"false"}';
			exit;
		}
        //echo '111';die;
    }
}
